 
package com.xbongbong.pro.statistic.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.pro.domain.dao.ChartCategoryDao;
import com.xbongbong.pro.domain.entity.ChartCategoryEntity;
import com.xbongbong.pro.statistic.enums.ChartCategoryTypeEnum;
import com.xbongbong.pro.statistic.model.ChartCategoryModel;
import com.xbongbong.sys.enums.SysEnvEnum;
import com.xbongbong.sys.help.SysEnvHelp;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;


/**
* @author autoGen
* @time 2019-01-07 11:13:40
*/
@Service("chartCategoryModel")
public class ChartCategoryModelImpl implements ChartCategoryModel {

	@Resource
	private ChartCategoryDao chartCategoryDao;
	@Resource
	private SysEnvHelp sysEnvHelp;

	@Override
	public Integer insert(ChartCategoryEntity entity){
		long now = DateUtil.getInt();
    	entity.setAddTime(now);
    	entity.setUpdateTime(now);
		return chartCategoryDao.insert(entity);
	}
	@Override
	public Integer update(ChartCategoryEntity entity){
		entity.setUpdateTime((long)DateUtil.getInt());
		
		return chartCategoryDao.update(entity);
	}

	@Override
	public Integer save(ChartCategoryEntity entity){
//		if (Objects.isNull(entity.getGray())) {
//			entity.setGray(SysEnvEnum.PROD.getCode());
//		}
		if(entity.getId() == null || entity.getId().equals(0L)) {
			return insert(entity);
		}
		return update(entity);
	}

	@Override
	public void insertBatch(List<ChartCategoryEntity> entityList) {
		chartCategoryDao.insertBatch(entityList);
    }
	@Override
	public Integer deleteByKey(Long key, String corpid){
		return chartCategoryDao.deleteByKey(key , corpid);
	}
	@Override
	public ChartCategoryEntity getByKey(Long key,  String corpid){
		return chartCategoryDao.getByKey(key , corpid);
	}

	/**
	 * Description:根据传参标记pushlink的数据为已读
	 *
	 * @param param 查询参数map
	 */
	@Override
	public void batchUpdateEnable(Map<String, Object> param) {
		chartCategoryDao.batchUpdateEnable(param);
	}

	@Override
	public List<ChartCategoryEntity> findEntitys(Map<String ,Object>  param){
//		sysEnvHelp.addGrayEnvironmentQueryCondition4Default(param);
		return chartCategoryDao.findEntitys(param);
	}

	@Override
	public Integer getEntitysCount(Map<String ,Object>  param){
		return chartCategoryDao.getEntitysCount(param);
	}

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
	public ChartCategoryEntity getEntityByAlias(String corpid, String alias) {
		Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
		param.put("corpid", corpid);
		param.put("del", DelEnum.NORMAL.getDel());
		param.put("alias", alias);
		List<ChartCategoryEntity> list =  findEntitys(param);
		if (list == null || list.isEmpty()) {
			return new ChartCategoryEntity();
		}
		return list.get(0);
	}

	@Override
	public List<ChartCategoryEntity> getList(String corpid) {
		Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
		param.put("corpid", corpid);
		param.put("del", DelEnum.NORMAL.getDel());
		//查询
		return findEntitys(param);
	}

	@Override
	public Map<String, String> getIdNameMap(String corpid) {
		List<ChartCategoryEntity> list = getList(corpid);
		//分类id-分类名称
		Map<String, String> idNameMap = new HashMap<>(list.size());
		for (ChartCategoryEntity entity : list) {
			idNameMap.put(entity.getId().toString(), entity.getName());
		}
		return idNameMap;
	}

	/**
	 * 根据别名查询
	 * @param param
	 * @author xingxing.xiao
	 */
	@Override
	public ChartCategoryEntity getEntityByAlias(Map<String, Object> param) {
		return chartCategoryDao.getEntityByAlias(param);
	}

	@Override
	public List<ChartCategoryEntity> getOverInitAlias(Map<String, Object> param) {
		//塞入要判断是否被重复初始化的系统分类
		param.put("typeIn", ChartCategoryTypeEnum.getCodeList4Init());
		return chartCategoryDao.getOverInitAlias(param);
	}

	@Override
	public Integer physicalDelete(String corpid, Collection<Long> idIn) {
		return chartCategoryDao.physicalDelete(corpid, idIn);
	}

	@Override
	public List<String> getCorpidList() {
		return chartCategoryDao.getCorpidList();
	}

	@Override
    public void updateBatch(List<ChartCategoryEntity> entityList, String corpid) {
		chartCategoryDao.updateBatch(entityList, corpid);
    }

	@Override
	public void updateName(Long id, String name) {
		chartCategoryDao.updateName(id, name);
	}

	@Override
	public void updatePublishFlag(Long categoryId, Integer publish, String corpid) {
		chartCategoryDao.updatePublishFlag(categoryId, publish, corpid);
	}

	@Override
	public void updateGlobalStyle(ChartCategoryEntity chartCategoryEntity) {
		chartCategoryEntity.setUpdateTime((long)DateUtil.getInt());
		chartCategoryDao.updateGlobalStyle(chartCategoryEntity);
	}

	@Override
	public void updateBatch4Script(List<ChartCategoryEntity> entityList, List<String> corpidList){
		chartCategoryDao.updateBatch4Script(entityList,corpidList);
	}

}

 
